﻿using Agile.Infra.Core.Guard;
using System.Security.Cryptography; 

namespace Agile.Infra.Core.Security
{
    public static class MD5Helper
    {
        /// <summary>对字符串进行MD5加密</summary>
        /// <param name="source">需要加密的字符串</param>
        /// <returns>MD5字符串</returns>
        public static string MD5String(string source) => MD5String(source, Encoding.UTF8);

        /// <summary>对字符串进行MD5加密</summary>
        /// <param name="source">需要加密的字符串</param>
        /// <param name="encoding">字符编码</param>
        /// <returns>MD5字符串</returns>
        public static string MD5String(string source, Encoding encoding)
        {
            Checker.Argument.IsNotEmpty(source, nameof(source));
            using MD5 md5 = MD5.Create();
            var buffer = encoding.GetBytes(source);
            var bytes = md5.ComputeHash(buffer);
            return bytes.Aggregate("", (current, b) => current + b.ToString("x2"));
        }

        /// <summary>对字符串进行三次MD5加密</summary>
        /// <param name="source">需要加密的字符串</param>
        /// <returns>MD5字符串</returns>
        public static string MD5String3(string source) => MD5String3(source, Encoding.UTF8);

        /// <summary>对字符串进行三次MD5加密</summary>
        /// <param name="source">需要加密的字符串</param>
        /// <param name="encoding">字符编码</param>
        /// <returns>MD5字符串</returns>
        public static string MD5String3(string source, Encoding encoding)
        {
            Checker.Argument.IsNotEmpty(source, nameof(source));
            using MD5 md5 = MD5.Create();
            var bytes = encoding.GetBytes(source);
            var bytes1 = md5.ComputeHash(bytes);
            var bytes2 = md5.ComputeHash(bytes1);
            var bytes3 = md5.ComputeHash(bytes2);
            return bytes3.Aggregate("", (current, b) => current + b.ToString("x2"));
        }
          
        /// <summary>64位MD5加密</summary>
        /// <param name="source">需要加密的字符串</param>
        /// <param name="salt">盐</param>
        /// <returns>MD5字符串</returns>
        public static string MD5StringBase64(string source) => MD5StringBase64(source, Encoding.UTF8);

        /// <summary>64位MD5加密</summary>
        /// <param name="source">需要加密的字符串</param>
        /// <param name="salt">盐</param>
        /// <returns>MD5字符串</returns>
        public static string MD5StringBase64(string source, Encoding encoding)
        {
            Checker.Argument.IsNotEmpty(source, nameof(source));
            using MD5 md5 = MD5.Create();
            var buffer = encoding.GetBytes(source);
            var bytes = md5.ComputeHash(buffer);
            return Convert.ToBase64String(bytes);
        }

        public static string Base64MD5(string sourcein)
        {
            var baseBuffer = Encoding.UTF8.GetBytes(sourcein.ToString());
            using MD5 md5 = MD5.Create();
            var sourceinData = md5.ComputeHash(Encoding.UTF8.GetBytes(Convert.ToBase64String(baseBuffer)));
            return sourceinData.Aggregate("", (current, b) => current + b.ToString("x2"));
        }
    }
}
